//The MIT License
//
//Copyright (c) 2009 nodchip
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
package tv.dyndns.kishibe.client.game;

import tv.dyndns.kishibe.client.Utility;
import tv.dyndns.kishibe.client.util.SerialTimer;

import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;

public class WidgetTimer extends VerticalPanel {
	private static final int WIDTH = 480;
	private static final String TEMPLATE = "<p style='position:relative;'><span class='timerLabel'>__second__</span><span style='position:absolute;top:20px;left:__left__;width:__width__;height:20px;background-color:__color__;z-index:10;'></span></p>";
	private static final int UPDATE_DURATION = 250;
	private static final int MAX_TIME = 30 * 1000;
	private final HTML html = new HTML();
	private int second = 30;
	private final long startTime = System.currentTimeMillis();

	private final Timer timer = new SerialTimer() {
		public void run() {
			final long currentTime = System.currentTimeMillis();
			final int time = (int) (currentTime - startTime);

			final int left = time * WIDTH / MAX_TIME;
			final int width = WIDTH - left;

			String text = TEMPLATE;
			if (second != 0) {
				text = text.replaceAll("__second__", "残り時間約" + second + "秒");
			} else {
				text = text.replaceAll("__second__", "時間切れ");
			}
			text = text.replaceAll("__left__", "" + left);
			text = text.replaceAll("__width__", "" + width);
			text = text.replaceAll("__color__", Utility.getInstance().createBackgroundColorString((double) width / (double) WIDTH));
			html.setHTML(text);
		}
	};

	public WidgetTimer() {
		setHorizontalAlignment(ALIGN_CENTER);
		setVerticalAlignment(ALIGN_MIDDLE);

		add(html);
		html.setPixelSize(WIDTH, 40);
	}

	public void setTimer(int second) {
		if (second <= 0) {
			this.second = 0;
			return;
		}

		this.second = second;
	}

	protected void onLoad() {
		super.onLoad();
		timer.scheduleRepeating(UPDATE_DURATION);
	}

	protected void onUnload() {
		timer.cancel();
		super.onUnload();
	}
}
